package edu.jd.movieplatform.security.c_admin.impl;

import edu.jd.movieplatform.common.Utils;
import edu.jd.movieplatform.security.c_admin.CAdmin;
import edu.jd.movieplatform.security.c_admin.CAdminDto;
import edu.jd.movieplatform.security.c_admin.CAdminService;
import edu.jd.movieplatform.security.c_admin.DelCAdminDto;
import edu.jd.movieplatform.security.c_admin.dao.CAdminDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service //说明这是一个受spring容器管理的业务组件(Bean)
@Transactional //说明本类的所有方法都是事务性
public class CAdminServiceImpl implements CAdminService {

    @Resource
    private CAdminDao cAdminDao;

    @Override
    public Map<String, Object> getCAdminPage(CAdminDto dto) {
        if(!dto.getM_status().equals("c_admin")){
            throw new RuntimeException("没有查询权限");
        }
        //对于仅有一条语句，并且直接返回结果，可以进一步简化
        Map<String, Object> page =Utils.getPage(dto, ()->cAdminDao.findCAdminListByCondition(dto));

        return page;
    }
    @Override
    public Map<String, Object> getNoCinemaCAdminPage(CAdminDto dto) {
        if(!dto.getM_status().equals("c_admin")){
            throw new RuntimeException("没有查询权限");
        }
        Map<String, Object> page =Utils.getPage(dto, ()->cAdminDao.findNoCAdminList(dto));

        return page;
    }

    @Override
    public void addCAdmin(CAdminDto dto) {
        CAdmin admin = getCAdminById(dto.getM_id());
        if(admin != null){
            throw new RuntimeException("已存在相同账号的用户！");
        }
        dto.setM_status("c_admin");
        dto.setM_pwd(DEFAULT_PASSWORD);

        if(dto.getCnm_id()>0 ) {
            if (cAdminDao.findCAdminINCinema(dto.getCnm_id()) <= 0) {
                cAdminDao.insertCAdmintoSYS(dto);
                cAdminDao.insertCAdmintoMR(dto);
                cAdminDao.updateCinema(dto);
            } else {
                throw new RuntimeException("该电影院已有管理员");
            }
        }else {
            cAdminDao.insertCAdmintoSYS(dto);
            cAdminDao.insertCAdmintoMR(dto);
        }
    }

    @Override
    public void updateCAdmin(CAdminDto dto) {

        if(dto.getCnm_id()>0 ) {
            if (cAdminDao.findCAdminINCinema(dto.getCnm_id()) <= 0) {
                cAdminDao.updateCAdmin(dto);
                cAdminDao.updateCinema(dto);
            } else {
                throw new RuntimeException("该电影院已有管理员");
            }
        }else {
            cAdminDao.updateCAdmin(dto);
            cAdminDao.emptyCinema(dto);
        }

    }

    @Override
    public void deleteCAdmin(DelCAdminDto[] delCAdmin) {
        if(delCAdmin==null){
            throw new RuntimeException("没有获得需要删除用户的账号！");
        }
        int cnt = findCinemaByAdminIds(delCAdmin);
        if(cnt >0 ){
            throw new RuntimeException("部分管理员已挂靠在电影院下");
        }
        cAdminDao.deleteCAdmin(delCAdmin);
    }



    @Override
    public CAdmin getCAdminById(String m_id) {
        CAdmin admin = cAdminDao.findCAdminById(m_id);
        return admin;
    }

    @Override
    public int findCinemaByAdminIds(DelCAdminDto[] delAdmin) {
        int cnt = cAdminDao.findCinemaByAdminIds(delAdmin);
        return cnt;
    }

    @Override
    public int findCinemaByAdmin(String m_id) {
        int cnt =cAdminDao.findCinemaByAdmin(m_id);
        return cnt;
    }

    @Override
    public void deleteOneCAdmin(String m_id) {
        int cnt = findCinemaByAdmin(m_id);
        if (cnt>0){
            throw new RuntimeException("已属于某电影院管理");
        }
        cAdminDao.deleteOneCAdmin(m_id);
    }



}
